package xml;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import dataStructures.cachedIndex.IdToRouteCachedIndex;

public class IdToRouteCachedIndexHandler extends DefaultHandler{
	
	private ArrayList<Integer> _currentRoute = new ArrayList<Integer>();
	private IdToRouteCachedIndex _index;
	private int _nodeCount = 0;
	
	public IdToRouteCachedIndexHandler(IdToRouteCachedIndex index)
	{
		_index = index;
	}
	
	
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
	{
		_currentRoute.add(_index.GetlabelId(qName));
		
		try
		{
			_index.SetNodesRoute(_nodeCount, _currentRoute);
		}
		catch(Exception e)
		{
			throw new SAXException(e.getMessage());
		}
	
		_nodeCount++;
		//PrintRouteLabels(_currentRoute);
		if (_nodeCount % 1000000 == 0)
		{
			System.out.println("second pass " + _nodeCount);
		}
	}
	 
	public void endElement(String uri, String localName,String qName) throws SAXException 
	{
		
		//trim the last id from the current path
		_currentRoute.remove(_currentRoute.size()-1);
		
	}
	
	private int GetNumberOfNodes()
	{
		return _nodeCount;
	}
}
